document.addEventListener("DOMContentLoaded", function () {
  // 检查当前页面
  checkCurrentPage();

  // 自动生成按钮
  document
    .getElementById("copyMethodBtn")
    .addEventListener("click", function () {
      sendCommand("copyMethod");
    });

  document
    .getElementById("copyClassBtn")
    .addEventListener("click", function () {
      sendCommand("copyClass");
    });

  // 手动表单切换
  document.getElementById("toggleForm").addEventListener("click", function () {
    const formContainer = document.getElementById("formContainer");
    if (formContainer.style.display === "flex") {
      formContainer.style.display = "none";
      this.textContent = "手动输入API信息";
    } else {
      formContainer.style.display = "flex";
      this.textContent = "关闭手动输入";
    }
  });

  // 手动生成按钮
  document
    .getElementById("copyMethodManualBtn")
    .addEventListener("click", function () {
      sendManualCommand("copyMethod");
    });

  document
    .getElementById("copyClassManualBtn")
    .addEventListener("click", function () {
      sendManualCommand("copyClass");
    });

  // 检查当前页面信息
  function checkCurrentPage() {
    chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
      if (tabs[0]) {
        // 检查当前页面是否是Apifox页面
        const isApifoxPage = tabs[0].url.includes("apifox");
        const pageInfo = document.getElementById("pageInfo");
        const currentPage = document.getElementById("currentPage");

        if (isApifoxPage) {
          pageInfo.style.display = "block";
          currentPage.textContent = "Apifox页面";
          currentPage.style.color = "#4caf50";

          // 向页面发送消息，获取更多信息
          chrome.tabs.sendMessage(
            tabs[0].id,
            { action: "checkPage" },
            function (response) {
              if (response) {
                currentPage.textContent = "Apifox页面: " + response.title;
              }
            }
          );
        } else {
          pageInfo.style.display = "block";
          currentPage.textContent = "非Apifox页面";
          currentPage.style.color = "#f44336";
        }
      }
    });
  }

  // 发送命令到当前标签页的content script
  function sendCommand(command) {
    chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
      chrome.tabs.sendMessage(
        tabs[0].id,
        { action: command },
        function (response) {
          if (response && response.success) {
            showStatus("代码已复制到剪贴板！");
          } else {
            let message = "操作失败";
            if (response && response.message) {
              message = response.message;
            }
            showStatus(message, true);
          }
        }
      );
    });
  }

  // 发送带有手动输入API信息的命令
  function sendManualCommand(command) {
    const apiInfo = {
      name: document.getElementById("apiName").value,
      description: document.getElementById("apiDesc").value,
      path: document.getElementById("apiPath").value,
      method: document.getElementById("apiMethod").value,
      hasParams: document.getElementById("hasParams").checked,
    };

    // 验证必填字段
    if (!apiInfo.path) {
      showStatus("请输入API路径", true);
      return;
    }

    // 如果方法名为空，自动生成
    if (!apiInfo.name) {
      // 需要在content.js中生成
      apiInfo.name = "";
    }

    chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
      chrome.tabs.sendMessage(
        tabs[0].id,
        {
          action: command,
          apiInfo: apiInfo,
        },
        function (response) {
          if (response && response.success) {
            showStatus("代码已复制到剪贴板！");
          } else {
            let message = "操作失败";
            if (response && response.message) {
              message = response.message;
            }
            showStatus(message, true);
          }
        }
      );
    });
  }

  // 显示状态信息
  function showStatus(message, isError = false) {
    const status = document.getElementById("status");
    status.textContent = message;
    status.style.display = "block";
    status.style.color = isError ? "#f44336" : "#4caf50";

    setTimeout(function () {
      status.style.display = "none";
    }, 3000);
  }
});
